package com.rbcs.borrow.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.GenericGenerator;

import com.rbcs.base.model.Item;
import com.rbcs.base.model.Project;
import com.rbcs.flow.model.Flow;
import com.rbcs.repay.model.RePay;
import com.systop.common.modules.dept.model.Dept;
import com.systop.common.modules.security.user.model.User;
import com.systop.core.model.BaseModel;

/***
 * 借款申请表
 * 
 * @author ShangHua
 */
@SuppressWarnings("serial")
@Entity
@Table(name = "borrows")
public class Borrow extends BaseModel {

	private Integer id;

	// 借款单号
	private String singleNo;

	// 借款理由
	private String title;

	// 借款金额
	private Double amount;

	// 部门
	private Dept dept;

	// 项目
	private Project project;

	// 预算科目
	private Item item;

	// 借款人
	private User borrower;

	// 借款日期
	private Date createDate;

	// 审批状态[部门和财务的审批]
	private String audStatus;

	// 出纳付款状态
	private String payStatus;

	// 已还款金额
	private Double rePayAmount;

	// 描述
	private String descn;

	// 借款对应的还款记录
	private Set<RePay> rePays;

	private List<Flow> flows = new ArrayList<Flow>(0);

	@Id
	@GeneratedValue(generator = "hibseq")
	@GenericGenerator(name = "hibseq", strategy = "hilo")
	@Column(name = "id", nullable = false)
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
	@JoinColumn(name = "borrower_id")
	public User getBorrower() {
		return borrower;
	}

	public void setBorrower(User borrower) {
		this.borrower = borrower;
	}

	@ManyToOne(cascade = {}, fetch = FetchType.EAGER)
	@JoinColumn(name = "dept_id")
	public Dept getDept() {
		return dept;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}

	@ManyToOne(cascade = {}, fetch = FetchType.EAGER)
	@JoinColumn(name = "project_id")
	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	@ManyToOne(cascade = {}, fetch = FetchType.EAGER)
	@JoinColumn(name = "item_id")
	public Item getItem() {
		return item;
	}

	public void setItem(Item item) {
		this.item = item;
	}

	public String getSingleNo() {
		return singleNo;
	}

	public void setSingleNo(String singleNo) {
		this.singleNo = singleNo;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Column(precision = 10, scale = 2)
	public Double getAmount() {
		return amount;
	}

	public void setAmount(Double amount) {
		this.amount = amount;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	@Column(name = "aud_status")
	public String getAudStatus() {
		return audStatus;
	}

	public void setAudStatus(String audStatus) {
		this.audStatus = audStatus;
	}

	@Column(name = "pay_status", columnDefinition = "char(1)")
	public String getPayStatus() {
		return payStatus;
	}

	public void setPayStatus(String payStatus) {
		this.payStatus = payStatus;
	}

	@Column(name = "repay_amount", precision = 10, scale = 2)
	public Double getRePayAmount() {
		return rePayAmount;
	}

	public void setRePayAmount(Double rePayAmount) {
		this.rePayAmount = rePayAmount;
	}

	public String getDescn() {
		return descn;
	}

	public void setDescn(String descn) {
		this.descn = descn;
	}

	@OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "borrow")
	public Set<RePay> getRePays() {
		return rePays;
	}

	public void setRePays(Set<RePay> rePays) {
		this.rePays = rePays;
	}

	@OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "borrow")
	public List<Flow> getFlows() {
		return flows;
	}

	public void setFlows(List<Flow> flows) {
		this.flows = flows;
	}

	@Transient
	public Flow getTopFlow() {
		return (flows.isEmpty()) ? null : flows.get(0);
	}

}
